LIBSECP256K1=secp256k1/libsecp256k1.la

$(LIBSECP256K1): $(wildcard secp256k1/src/*) $(wildcard secp256k1/include/*)
	$(AM_V_at)$(MAKE) $(AM_MAKEFLAGS) -C $(@D) $(@F)

noinst_LTLIBRARIES =

include_HEADERS =
include_HEADERS += $(top_srcdir)/include/wally.hpp
include_HEADERS += $(top_srcdir)/include/wally_address.h
include_HEADERS += $(top_srcdir)/include/wally_bip32.h
include_HEADERS += $(top_srcdir)/include/wally_bip38.h
include_HEADERS += $(top_srcdir)/include/wally_bip39.h
include_HEADERS += $(top_srcdir)/include/wally_core.h
include_HEADERS += $(top_srcdir)/include/wally_crypto.h
include_HEADERS += $(top_srcdir)/include/wally_elements.h
include_HEADERS += $(top_srcdir)/include/wally_psbt.h
include_HEADERS += $(top_srcdir)/include/wally_script.h
include_HEADERS += $(top_srcdir)/include/wally_transaction.h

pkgconfigdir = $(libdir)/pkgconfig
pkgconfig_DATA = wallycore.pc

if BUILD_ELEMENTS
SWIG_GEN_FLAGS := -DBUILD_ELEMENTS=1
TOOLS_EXTRA_ARGS := "elements"
else
SWIG_GEN_FLAGS :=
TOOLS_EXTRA_ARGS :=
endif

if USE_SWIG_PYTHON
noinst_LTLIBRARIES += libswig_python.la
libswig_python_la_SOURCES = swig_python/swig_python_wrap.c

libswig_python_la_CFLAGS = -I$(top_srcdir) $(AM_CFLAGS) $(SWIG_PYTHON_CPPFLAGS) $(SWIG_WARN_CFLAGS) $(NOALIAS_CFLAGS)
libswig_python_la_LIBADD = $(PYTHON_LIBS)

# We must sed the wrapper to workaround SWIGs odd library naming conventions
swig_python/swig_python_wrap.c : swig_python/swig.i swig_python/python_extra.py_in
	$(AM_V_at)$(SWIG) $(SWIG_PYTHON_OPT) $(SWIG_GEN_FLAGS) -outdir swig_python -o $@ $< && \
    $(GNU_SED) -i 's/_wallycore/libwallycore/g' swig_python/wallycore.py $@ && \
    cat swig_python/wallycore.py swig_python/python_extra.py_in > swig_python/wallycore/__init__.py && \
    rm swig_python/wallycore.py

PYTHON_SWIGTEST = PYTHONDONTWRITEBYTECODE=1 PYTHONPATH=.libs:swig_python $(PYTHON)

clean-swig-python:
	$(AM_V_at)rm -f swig_python/swig_python_wrap.c swig_python/wallycore/__init__.py
clean-local: clean-swig-python

if IS_OSX
# Python requires the shared library have a .so extension for 'import'
.libs/libwallycore.so:
	$(AM_V_at)ln -s libwallycore.dylib .libs/libwallycore.so
SWIG_PYTHON_TEST_DEPS = .libs/libwallycore.so
endif

endif # USE_SWIG_PYTHON

if RUN_PYTHON_TESTS
PYTHON_TEST = PYTHONDONTWRITEBYTECODE=1 $(PYTHON)
endif

JS_WRAP_DEPS = wrap_js/makewrappers/wrap.py wrap_js/src/combined.c $(include_HEADERS)

# The various JS/Cordova wrappers are generated by makewrappers/wrap.py
wrap_js/nodejs_wrap.cc: $(JS_WRAP_DEPS) wrap_js/makewrappers/templates/nan.py
	PYTHONDONTWRITEBYTECODE=1 $(PYTHON) wrap_js/makewrappers/wrap.py nodejs @NODE_GYP_DIR@ $(TOOLS_EXTRA_ARGS)

wrap_js/wally.js: $(JS_WRAP_DEPS) wrap_js/makewrappers/templates/js.py
	PYTHONDONTWRITEBYTECODE=1 $(PYTHON) wrap_js/makewrappers/wrap.py wally @NODE_GYP_DIR@ $(TOOLS_EXTRA_ARGS)

wrap_js/cordovaplugin/wally.js: wrap_js/wally.js
	cp $^ $@

wrap_js/cordovaplugin/WallyCordova.java: $(JS_WRAP_DEPS) wrap_js/makewrappers/templates/java.py
	PYTHONDONTWRITEBYTECODE=1 $(PYTHON) wrap_js/makewrappers/wrap.py cordova-java @NODE_GYP_DIR@ $(TOOLS_EXTRA_ARGS)

wrap_js/cordovaplugin/WallyCordova.swift: $(JS_WRAP_DEPS) wrap_js/makewrappers/templates/swift.py
	PYTHONDONTWRITEBYTECODE=1 $(PYTHON) wrap_js/makewrappers/wrap.py cordova-swift @NODE_GYP_DIR@ $(TOOLS_EXTRA_ARGS)

cordova_wrap_files = wrap_js/cordovaplugin/wally.js wrap_js/cordovaplugin/WallyCordova.java wrap_js/cordovaplugin/WallyCordova.swift
if USE_SWIG_JAVA
cordova_wrap_files += wrap_js/cordovaplugin/Wally.java
wrap_js/cordovaplugin/Wally.java: swig_java/src/com/blockstream/libwally/Wally.java
	cp $^ $@
endif
cordova-wrappers: $(cordova_wrap_files)

if USE_JS_WRAPPERS
# The node wrapper is built by node-gyp
NODE_GYP = node_modules/.bin/node-gyp
wrap_js/build/@NODE_GYP_DIR@/wallycore.node: wrap_js/nodejs_wrap.cc wrap_js/wally.js $(lib_LTLIBRARIES) wrap_js/binding.gyp
	cd wrap_js && LIBWALLY_DIR=../.. npm_config_debug=$(shell [ @NODE_GYP_DIR@ == "Debug" ] && echo 1) yarn install

if BUILD_ELEMENTS
wrap_js/binding.gyp: wrap_js/binding.gyp.tmpl
	sed -e 's/"defines": \[ "SWIG_JAVASCRIPT_BUILD", "HAVE_CONFIG_H" \]/"defines": \[ "SWIG_JAVASCRIPT_BUILD", "HAVE_CONFIG_H","BUILD_ELEMENTS" \]/g' $< > $@
else
wrap_js/binding.gyp: wrap_js/binding.gyp.tmpl
	cp $< $@
endif

bin_PROGRAMS = wrap_js/build/@NODE_GYP_DIR@/wallycore.node

# FIXME: Run the test(s) as part of 'make check'

clean-js-wrappers:
	$(AM_V_at)rm -f wrap_js/nodejs_wrap.cc wrap_js/wally.js $(cordova_wrap_files)
clean-local: clean-js-wrappers
endif # USE_JS_WRAPPERS

if USE_SWIG_JAVA

noinst_LTLIBRARIES += libswig_java.la
libswig_java_la_SOURCES = \
    swig_java/swig_java_wrap.c

libswig_java_la_CFLAGS = -I$(top_srcdir) $(AM_CFLAGS) $(SWIG_JAVA_CPPFLAGS) $(SWIG_WARN_CFLAGS)

SWIG_JOPT = $(SWIG_JAVA_OPT) -outdir swig_java -noproxy -package com.blockstream.libwally

swig_java/swig_java_wrap.c: swig_java/swig.i swig_java/jni_extra.java_in
	$(AM_V_at)$(SWIG) $(SWIG_JOPT) $(SWIG_GEN_FLAGS) -o $@ $< && \
    $(GNU_SED) -i 's/wallycoreJNI/Wally/g' $@ && \
    $(GNU_SED) -i 's/ = (char \*) (\*jenv)->Get/ = (unsigned char \*) (\*jenv)->Get/g' $@

cblw=com/blockstream/libwally
cbt=com/blockstream/test
sjs=swig_java/src
$(sjs)/$(cblw)/Wally.java: swig_java/swig_java_wrap.c
	$(AM_V_at)$(top_srcdir)/tools/swigjavapost.sh @GNU_SED@ $(TOOLS_EXTRA_ARGS)

$(sjs)/$(cblw)/Wally.class: $(sjs)/$(cblw)/Wally.java
	$(AM_V_at)$(JAVAC) -implicit:none -source $(JAVAC_TARGET) -target $(JAVAC_TARGET) -sourcepath $(sjs)/$(cblw)/ $(sjs)/$(cblw)/Wally.java

swig_java/wallycore.jar: $(sjs)/$(cblw)/Wally.class
	$(AM_V_at)$(JAR) cf swig_java/wallycore.jar -C $(sjs) '$(cblw)/Wally$$Obj.class' -C $(sjs) '$(cblw)/Wally.class'

$(sjs)/$(cbt)/%.class: $(sjs)/$(cbt)/%.java swig_java/wallycore.jar
	$(AM_V_at)$(JAVAC) -implicit:none -source $(JAVAC_TARGET) -target $(JAVAC_TARGET) -sourcepath $(sjs) $(filter %.java,$^)

if HAVE_JAVAC
all: swig_java/wallycore.jar

SWIG_JAVA_TEST_DEPS = \
    $(sjs)/$(cbt)/test_bip32.class \
    $(sjs)/$(cbt)/test_tx.class \
    $(sjs)/$(cbt)/test_scripts.class \
    $(sjs)/$(cbt)/test_mnemonic.class

if BUILD_ELEMENTS
SWIG_JAVA_TEST_DEPS += $(sjs)/$(cbt)/test_assets.class \
    $(sjs)/$(cbt)/test_pegs.class
endif

all: $(SWIG_JAVA_TEST_DEPS)

JAVA_TEST = @LDPATH_VAR@=.libs $(JAVA) -Djava.library.path=.libs -classpath $(sjs) com.blockstream.test.

endif # HAVE_JAVAC

clean-swig-java:
	$(AM_V_at)rm -rf swig_java/swig_java_wrap.c $(sjs)/$(cblw) $(sjs)/$(cbt)/*.class swig_java/*.java swig_java/*.jar
clean-local: clean-swig-java
endif # USE_SWIG_JAVA

lib_LTLIBRARIES = libwallycore.la

libwallycore_la_SOURCES = \
    address.c \
    aes.c \
    base58.c \
    bip32.c \
    bip38.c \
    bip39.c \
    bech32.c \
    ecdh.c \
    elements.c \
    blech32.c \
    hex.c \
    hmac.c \
    internal.c \
    mnemonic.c \
    pbkdf2.c \
    psbt.c \
    script.c \
    scrypt.c \
    sign.c \
    symmetric.c \
    transaction.c \
    wif.c \
    wordlist.c \
    ccan/ccan/base64/base64.c \
    ccan/ccan/crypto/ripemd160/ripemd160.c \
    ccan/ccan/crypto/sha256/sha256.c \
    ccan/ccan/crypto/sha512/sha512.c \
    ccan/ccan/str/hex/hex.c

libwallycore_la_INCLUDES = \
    include/wally.hpp \
    include/wally_address.h \
    include/wally_bip32.h \
    include/wally_bip38.h \
    include/wally_bip39.h \
    include/wally_core.h \
    include/wally_crypto.h \
    include/wally_elements.h \
    include/wally_psbt.h \
    include/wally_script.h \
    include/wally_symmetric.h \
    include/wally_transaction.h

libwallycore_la_CFLAGS = -I$(top_srcdir) -I$(srcdir)/ccan -DWALLY_CORE_BUILD=1 $(AM_CFLAGS)
libwallycore_la_LIBADD = $(LIBADD_SECP256K1) $(noinst_LTLIBRARIES)

SUBDIRS = secp256k1

TESTS =
noinst_PROGRAMS =
if RUN_TESTS
TESTS += test_bech32
noinst_PROGRAMS += test_bech32
test_bech32_SOURCES = ctest/test_bech32.c
test_bech32_CFLAGS = -I$(top_srcdir)/include $(AM_CFLAGS)
test_bech32_LDADD = $(lib_LTLIBRARIES) @CTEST_EXTRA_STATIC@
if USE_PTHREAD
TESTS += test_clear
noinst_PROGRAMS += test_clear
test_clear_SOURCES = ctest/test_clear.c
test_clear_CFLAGS = -I$(top_srcdir)/include $(PTHREAD_CFLAGS) $(AM_CFLAGS)
test_clear_LIBS = $(PTHREAD_LIBS)
test_clear_LDADD = $(lib_LTLIBRARIES) @CTEST_EXTRA_STATIC@
endif
TESTS += test_tx
noinst_PROGRAMS += test_tx
test_tx_SOURCES = ctest/test_tx.c
test_tx_CFLAGS = -I$(top_srcdir)/include $(AM_CFLAGS)
test_tx_LDADD = $(lib_LTLIBRARIES) @CTEST_EXTRA_STATIC@
if BUILD_ELEMENTS
TESTS += test_elements_tx
noinst_PROGRAMS += test_elements_tx
test_elements_tx_SOURCES = ctest/test_elements_tx.c
test_elements_tx_CFLAGS = -I$(top_srcdir)/include $(AM_CFLAGS)
test_elements_tx_LDADD = $(lib_LTLIBRARIES) @CTEST_EXTRA_STATIC@
TESTS += test_blech32
noinst_PROGRAMS += test_blech32
test_blech32_SOURCES = ctest/test_blech32.c
test_blech32_CFLAGS = -I$(top_srcdir)/include $(AM_CFLAGS)
test_blech32_LDADD = $(lib_LTLIBRARIES) @CTEST_EXTRA_STATIC@
endif

check-local: $(SWIG_PYTHON_TEST_DEPS) $(SWIG_JAVA_TEST_DEPS)
if SHARED_BUILD_ENABLED
if RUN_PYTHON_TESTS
	$(AM_V_at)$(PYTHON_TEST) test/test_address.py
	$(AM_V_at)$(PYTHON_TEST) test/test_aes.py
	$(AM_V_at)$(PYTHON_TEST) test/test_base58.py
	$(AM_V_at)$(PYTHON_TEST) test/test_bech32.py
	$(AM_V_at)$(PYTHON_TEST) test/test_bip32.py
	$(AM_V_at)$(PYTHON_TEST) test/test_bip38.py
	$(AM_V_at)$(PYTHON_TEST) test/test_bip39.py
	$(AM_V_at)$(PYTHON_TEST) test/test_ecdh.py
	$(AM_V_at)$(PYTHON_TEST) test/test_elements.py
	$(AM_V_at)$(PYTHON_TEST) test/test_hash.py
	$(AM_V_at)$(PYTHON_TEST) test/test_hex.py
	$(AM_V_at)$(PYTHON_TEST) test/test_hmac.py
	$(AM_V_at)$(PYTHON_TEST) test/test_mnemonic.py
	$(AM_V_at)$(PYTHON_TEST) test/test_psbt.py
	$(AM_V_at)$(PYTHON_TEST) test/test_pbkdf2.py
	$(AM_V_at)$(PYTHON_TEST) test/test_script.py
	$(AM_V_at)$(PYTHON_TEST) test/test_scrypt.py
	$(AM_V_at)$(PYTHON_TEST) test/test_sign.py
	$(AM_V_at)$(PYTHON_TEST) test/test_transaction.py
	$(AM_V_at)$(PYTHON_TEST) test/test_wif.py
	$(AM_V_at)$(PYTHON_TEST) test/test_wordlist.py
if USE_SWIG_PYTHON
	$(AM_V_at)$(PYTHON_SWIGTEST) swig_python/contrib/address.py
	$(AM_V_at)$(PYTHON_SWIGTEST) swig_python/contrib/bip32.py
	$(AM_V_at)$(PYTHON_SWIGTEST) swig_python/contrib/mnemonic.py
	$(AM_V_at)$(PYTHON_SWIGTEST) swig_python/contrib/sha.py
	$(AM_V_at)$(PYTHON_SWIGTEST) swig_python/contrib/tx.py
if BUILD_ELEMENTS
	$(AM_V_at)$(PYTHON_TEST) test/test_confidential_addr.py
	$(AM_V_at)$(PYTHON_TEST) test/test_pegin.py
	$(AM_V_at)$(PYTHON_TEST) test/test_pegout.py
	$(AM_V_at)$(PYTHON_SWIGTEST) swig_python/contrib/elements_tx.py
	$(AM_V_at)$(PYTHON_SWIGTEST) pyexample/liquid/receive-send.py
endif
endif
endif # RUN_PYTHON_TESTS
if RUN_JAVA_TESTS
if BUILD_ELEMENTS
	$(AM_V_at)$(JAVA_TEST)test_assets
	$(AM_V_at)$(JAVA_TEST)test_pegs
endif
	$(AM_V_at)$(JAVA_TEST)test_bip32
	$(AM_V_at)$(JAVA_TEST)test_mnemonic
	$(AM_V_at)$(JAVA_TEST)test_scripts
	$(AM_V_at)$(JAVA_TEST)test_tx
endif
if USE_JS_WRAPPERS
if BUILD_ELEMENTS
	$(AM_V_at)wrap_js/node_modules/.bin/tape wrap_js/test/test_assets.js
endif
	$(AM_V_at)wrap_js/node_modules/.bin/tape wrap_js/test/test_aes.js
	$(AM_V_at)wrap_js/node_modules/.bin/tape wrap_js/test/test_base58.js
	$(AM_V_at)wrap_js/node_modules/.bin/tape wrap_js/test/test_bip32.js
	$(AM_V_at)wrap_js/node_modules/.bin/tape wrap_js/test/test_bip38.js
	$(AM_V_at)wrap_js/node_modules/.bin/tape wrap_js/test/test_bip39.js
	$(AM_V_at)wrap_js/node_modules/.bin/tape wrap_js/test/test_hash.js
	$(AM_V_at)wrap_js/node_modules/.bin/tape wrap_js/test/test_scrypt.js
	$(AM_V_at)wrap_js/node_modules/.bin/tape wrap_js/test/test_script.js
endif
endif # SHARED_BUILD_ENABLED
endif # RUN_TESTS

.PHONY: clean-swig-python clean-js-wrappers clean-swig-java cordova-wrappers
